package Main;
import java.util.ArrayList;

import algorithm.Entropy;


public class Attribute {
	ArrayList<String> data ;
	ArrayList<String> dataTypes ;
	private String name;
	private String a;
	
	public Attribute(String name){
		data = new ArrayList<String>();
		dataTypes = new ArrayList<String>();
		this.name = name;
	}
	public void addData(String inData) {
			data.add(inData);
		
	  }
	
	public int nbrData(){
		return data.size();
	}
	
	public String getGeneric(){
	    
	    
	    return null;
	    
	}
	
	public ArrayList<String> getData(){
		return data;
	}
	public void addDataType(String type){
		dataTypes.add(type);
	}
	
	public String toString(){
		String s = name + ": ";
		for(String t : data){
			s += t + " ";
		}
	return s;
	}
	
	

	
	public int getNrbDataTypes(){
		return dataTypes.size();
	}
	
	
	public ArrayList<String> getDataTypes(){
		return dataTypes;
	}
	
	public String getDataAt(int i){
		return data.get(i);
	}
	public String getName(){
		return name;
	}
	

	
	public double calculateEntropy(Attribute goal,ArrayList<Integer> indexSet){
		
		double[] entropy = new double[dataTypes.size()];
		
		double[][] nbr = new double[dataTypes.size()][2];
		
		
		
		
		for(int i = 0; i < indexSet.size(); i++){
			String temp = (String)data.get(indexSet.get(i));
			
			for(int j = 0; j <dataTypes.size();j++){
				if(temp.equals(dataTypes.get(j))){					
					ArrayList<String> goalDataTypes = goal.getDataTypes();
					if(goalDataTypes.get(0).equals((String)goal.getDataAt(indexSet.get(i)))){
						nbr[j][0] = nbr[j][0] + 1;
					}else if( goalDataTypes.get(1).equals((String)goal.getDataAt(indexSet.get(i)))){
						nbr[j][1] = nbr[j][1] + 1;
					}
					
					
				}
				
			}
		
		}
		
		
		
		for(int i = 0; i  < entropy.length; i++){
			entropy[i] = Entropy.calc(nbr[i][0], nbr[i][1]);
			if( Double.isNaN(entropy[i])){
				
				entropy[i] = 0;
			}
		}
		
		
		double dataSetEntropy = goalEntropy(goal);
		
		if(Double.isNaN(dataSetEntropy)){
			dataSetEntropy = 0;
		}
		double attributeEntropy = 0.0;
		for(int i = 0; i <entropy.length;i++){
			attributeEntropy += entropy[i]*(nbr[i][0] +nbr[i][1])/ data.size();
		}
		attributeEntropy = dataSetEntropy - attributeEntropy;

		return attributeEntropy;
	}
	
	
	private double goalEntropy(Attribute g){
		ArrayList<String> a = g.getDataTypes();
		int first = 0;
		int second = 0;
		ArrayList<String> b = g.getData();
		for(int i = 0; i < b.size(); i++ ){
			if(a.get(0).equals((String)g.getDataAt(i))){
				first ++;
			}else if( a.get(1).equals((String)g.getDataAt(i))){
				second ++;
			}
		}
		return Entropy.calc(first, second);
	}
	

	
}
